package fun.tan90.device.main.rest;

import fun.tan90.device.event.EventTopic;
import fun.tan90.device.event.model.DeviceSourceData;
import fun.tan90.device.main.model.dataobject.db1.DeviceInfo;
import fun.tan90.device.main.service.DeviceInfoService;
import org.noear.dami.Dami;
import org.noear.snack.ONode;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;

import java.util.Map;

@Controller
@Mapping("/demo")
public class DemoController {

    @Inject
    DeviceInfoService deviceInfoService;

    @Mapping("/device-info")
    public DeviceInfo deviceInfo(String deviceCode) {
        return deviceInfoService.deviceInfo(deviceCode);
    }

    @Mapping("/send")
    public void send() {
        Dami.<Map<String, Object>, Void>bus().send(EventTopic.DEVICE_TRANSFER_DATA_TOPIC + "123", Map.of("age", "20"));
    }

    @Mapping("/protocol")
    public ONode protocol(String dataSource, String data) {
        return Dami.<DeviceSourceData, ONode>bus().sendAndRequest(EventTopic.PROTOCOL_GRAAL_EVAL_TOPIC,
                new DeviceSourceData("demo", dataSource, "12", data));
    }
}